"""深度检查项目代码"""
import sys

print("=" * 70)
print("Reddbook 项目代码深度检查")
print("=" * 70)

# 1. 检查配置
print("\n[1/7] 检查配置模块...")
try:
    from config import settings
    print("✅ config.py 导入成功")
    print(f"   - DINGTALK_APP_KEY: {settings.DINGTALK_APP_KEY}")
    print(f"   - DINGTALK_REDIRECT_URI: {settings.DINGTALK_REDIRECT_URI}")
    print(f"   - DATABASE_URL: {settings.DATABASE_URL[:50]}...")
except Exception as e:
    print(f"❌ config.py 导入失败: {e}")
    sys.exit(1)

# 2. 检查数据库
print("\n[2/7] 检查数据库模块...")
try:
    from database import engine, SessionLocal, init_db
    print("✅ database.py 导入成功")
except Exception as e:
    print(f"❌ database.py 导入失败: {e}")
    sys.exit(1)

# 3. 检查模型
print("\n[3/7] 检查数据模型...")
try:
    from models.user import User
    print("✅ models/user.py 导入成功")
    print(f"   - User表字段数: {len(User.__table__.columns)}")
except Exception as e:
    print(f"❌ models/user.py 导入失败: {e}")
    sys.exit(1)

# 4. 检查工具函数
print("\n[4/7] 检查工具函数...")
try:
    from tools.auth import create_access_token, verify_token
    print("✅ tools/auth.py 导入成功")
except Exception as e:
    print(f"❌ tools/auth.py 导入失败: {e}")
    sys.exit(1)

# 5. 检查钉钉工具
print("\n[5/7] 检查钉钉工具...")
try:
    from tools.dingtalk import DingtalkClient, dingtalk_client, generate_dingtalk_auth_url
    print("✅ tools/dingtalk.py 导入成功")
    
    # 测试生成授权URL
    url, state = generate_dingtalk_auth_url()
    print(f"   - 授权URL生成测试: {len(url)} 字符")
    print(f"   - State: {state}")
except Exception as e:
    print(f"❌ tools/dingtalk.py 导入失败: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# 6. 检查视图
print("\n[6/7] 检查API视图...")
try:
    from views.auth import router
    print("✅ views/auth.py 导入成功")
    print(f"   - 路由数量: {len(router.routes)}")
except Exception as e:
    print(f"❌ views/auth.py 导入失败: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# 7. 检查主应用
print("\n[7/7] 检查主应用...")
try:
    from main import app
    print("✅ main.py 导入成功")
    print(f"   - 应用标题: {app.title}")
    print(f"   - 路由数量: {len(app.routes)}")
except Exception as e:
    print(f"❌ main.py 导入失败: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# 总结
print("\n" + "=" * 70)
print("✅ 所有模块检查通过！项目代码正常")
print("=" * 70)
print("\n当前配置：")
print(f"  - AppKey: {settings.DINGTALK_APP_KEY}")
print(f"  - 回调URL: {settings.DINGTALK_REDIRECT_URI}")
print(f"  - 数据库: {settings.DATABASE_URL.split('@')[1] if '@' in settings.DATABASE_URL else 'N/A'}")
print("\n建议：")
print("  1. 确保钉钉开放平台配置了回调域名")
print("  2. 确认应用类型为企业内部应用")
print("  3. 访问 http://localhost:5173/login 测试登录")

